<?php
class UsersController extends AppController {

	var $name = 'Users';
    var $components = array('Auth','Session','common','Email','RequestHandler');

    function beforeFilter(){
        parent::beforeFilter();
        $this->Auth->allow('index');
    }

	function index() {
	   $a = $this->Session->read('cur_id');
       echo $a;
		$this->User->recursive = 0;
		$this->set('users', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid user', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('user', $this->User->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->User->create();
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('The user has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
			}
		}
		$groups = $this->User->Group->find('list');
		$this->set(compact('groups'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid user', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('The user has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->User->read(null, $id);
		}
		$groups = $this->User->Group->find('list');
		$this->set(compact('groups'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for user', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->User->delete($id)) {
			$this->Session->setFlash(__('User deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('User was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
    
    function login()
		{ 
		$user = $this->User->find('first', array('conditions'=>array('username'=>$this->data['User']['username'], 
                'password'=>md5($this->data['User']['password']))));
		}
		        
    function logout()
        {
            $this->redirect($this->Auth->logout());
        }
    

 	function doimk() 
		{
		  if(!empty($this->data)) {
          $a = $this->Session->read('cur_id');
          //$b = $this->Session->read('ho_ten');
          //echo $b;
          $user =$this->User->find('first', array('conditions'=>array('User.id'=>$a, 
                'password'=>md5($this->data['User']['old_password']))));
                 
		  if ($user)
                {
                //$a = md5($this->data['User']['new_password'])
				//$record['User']['password']= md5($this->data['User']['new_password']);
                //debug($record['User']['password']);
                //$record['User']['username']= $b;
				//$record['User']['id'] = $a;
                //echo $a;
                //echo $b;
                //debug($record);
                
                $this->User->id = $a ; 
                $pass = md5($this->data['User']['new_password']);
				if($this->User->saveField('password',$pass))
                //if $this->User->saveField('username',$a)
				{
					$this->Session->setFlash("Đổi mật khẩu thành công.");
					$this->redirect('/users');
				}
                else $this->Session->setFlash('thất bại');
			}
          else $this->Session->setFlash("Mat khau cu ko dung");
		}
        }
        
        function __sendPasswordEmail($user, $password) {
                if ($user === false) {
                echo 'failed to retrieve User data for user.id: '.$user['User']['id'];
                return false;
                }
                $this->set('user', $user['User']);
                $this->set('password', $password);
                $this->Email->to = $user['User']['email']; //email nguoi dung
                $this->Email->subject = 'Password Change Request';// chu de cua email
                $this->Email->from = 'No-reply noreply@your-domain.com>'; //email cua nguoi goi
                return $this->Email->send('Your new password is: '.$password);
        }
        
        function createTempPassword() 
            {
	           $pass = rand(100000,999999);
	           return $pass;
	       }
        
        function forgot() {
	       if(!empty($this->data)) {
		  $user = $this->User->findByEmail($this->data['User']['email']);
		  if($user) {
		      $a = $this->Session->read('cur_id');
              //$this->User->id = $a ; 
              $record['User']['id'] = $a;
              //echo $a;
		      $newpass = $this->createTempPassword();
		      $record['User']['password'] = $this->Auth->password($newpass);
		      if($this->User->saveAll($record)) {
			     $this->__sendPasswordEmail($user, $newpass);
			     $this->Session->setFlash('A new password was sent to your email');
			     $this->redirect($this->referer());
		          }
		      } else {
		          $this->Session->setFlash('Email is not existed!');
		}
	  }
	}
 
    
}
